﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;

public class MixerController : MonoBehaviour {

    public AudioMixer audioMixer;

    public Button buttonSnapDefault;
    public Button buttonSnapMute;
    public Slider masterVolumeSlider;
    public Slider musicVolumeSlider;
    
    private AudioMixerSnapshot defaultSnapShot;
    private AudioMixerSnapshot muteMusicSnapShot;

    public void OnSnapDefault()
    {
        buttonSnapDefault.interactable = false;
        buttonSnapMute.interactable = true;

        if ( null != defaultSnapShot )
        {
            defaultSnapShot.TransitionTo(0.5F);
        }
    }

    public void OnSnapMute()
    {
        buttonSnapDefault.interactable = true;
        buttonSnapMute.interactable = false;

        if ( null != muteMusicSnapShot )
        {
            muteMusicSnapShot.TransitionTo(0.5F);
        }
    }

    /// <summary>
    /// 设置主音量
    /// </summary>
    /// <param name="value">[0..1]</param>
    public void OnMasterVolume(float value)
    {
        if (null == audioMixer || null == masterVolumeSlider)
        {
            return;
        }

        float vol = (value * 80) - 80;
        ///
        /// 参数的创建：
        /// 1.选择一个AudioMixerGroup
        /// 2.在Inspector面板中，在具体的属性名称上单击鼠标右键
        /// 选择expose...
        /// 3.创建的参数在AudioMixer面板的右上角有下拉（可修改名称）
        /// 4.已经被expose的参数，在名称上有个向右的箭头可以识别出来
        /// 5.可以使用unexpose来删除参数
        audioMixer.SetFloat("volumeOfMaster", vol);
    }

    /// <summary>
    /// 设置音乐音量
    /// </summary>
    /// <param name="value">[0..1]</param>
    public void OnMusicVolume(float value)
    {
        if (null == audioMixer || null == musicVolumeSlider)
        {
            return;
        }

        float vol = (value * 80) - 80;
        audioMixer.SetFloat("volumeOfMusic", vol);
    }

    public void OnVoiceVolume(float value)
    {
        if (null == audioMixer || null == musicVolumeSlider)
        {
            return;
        }

        float vol = (value * 80) - 80;
        audioMixer.SetFloat("volumeOfVoice", vol);
    }

    private void UpdateMasterVolumeSlider()
    {
        if (null == audioMixer)
        {
            return;
        }

        bool flag = false;
        float volume = 0;//[-80..0]
        float value = 0;//[0..1]

        if (null != masterVolumeSlider)
        {
            flag = audioMixer.GetFloat("volumeOfMaster", out volume);
            if (flag)
            {
                value = (volume + 80) / 80;
                masterVolumeSlider.value = value;
            }
        }
        else
        {
            masterVolumeSlider.interactable = false;
        }

        if (null != musicVolumeSlider)
        {
            flag = audioMixer.GetFloat("volumeOfMusic", out volume);
            if (flag)
            {
                value = (volume + 80) / 80;
                musicVolumeSlider.value = value;
            }
        }
        else
        {
            musicVolumeSlider.interactable = false;
        }
    }

    // Use this for initialization
    void Start () {
        if (null == audioMixer)
        {
            return;
        }

        AudioMixerGroup group = audioMixer.outputAudioMixerGroup;
        if (null != group)
        {
            Debug.Log(group.name);
        }

        Debug.Log(" === subs from Root === ");
        AudioMixerGroup[] subGroups = audioMixer.FindMatchingGroups("");
        foreach (AudioMixerGroup item in subGroups)
        {
            Debug.Log(item.name);
        }

        Debug.Log(" === subs from Master === ");
        subGroups = audioMixer.FindMatchingGroups("Master/");
        foreach (AudioMixerGroup item in subGroups)
        {
            Debug.Log(item.name);
        }

        defaultSnapShot = audioMixer.FindSnapshot("Snapshot");
        muteMusicSnapShot = audioMixer.FindSnapshot("Snapshot - MuteMusic");

        if (null != defaultSnapShot)
        {
            Debug.Log("找到默认快照");
        }
        if (null != muteMusicSnapShot)
        {
            Debug.Log("找到音乐静音快照");
        }

        UpdateMasterVolumeSlider();
    }
	
	// Update is called once per frame
	void Update () {
		
	}
}
